/*
作者：chen
时间：2018-09-30
描述：我的
*/

//获取本地信息
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");

//页面刷新
window.addEventListener('refresh_meathod', function(e) { //执行刷新 
	location.reload();
});

//商家中心
function levelInfo() {
	//查询用户信息接口
	mui.ajax(user_url + '/api/userinfo', {
		data: {
			"phone": my_phone,
			"sign": "string",
			"user_id": user_id
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.code == 10000) {
				$('#my_business_center_user').text(data.resp.phone);
				$('#my_user_btn').text(data.resp.levelString);
				//$("#mui_pull_left_img").attr("src", data.resp.nvitation_code);
			} else {
				mui.alert(data.msg);
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
		}
	});

	//等级信息接口
	mui.ajax(user_url + '/api/levelInfo', {
		data: {},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.code == 10000) {
				var html = '';
				for(var i = 2; i < data.resp.length; i++) {
					html += '<li class="mui-table-view-cell">';
					html += '<a href="javascript:;">';
					html += '<span class="my_business_category">' + data.resp[i].level_name + '</span>';
					html += '<span class="my_business_moneynum"><span>' + data.resp[i].level_money + '</span>元</span>';
					html += '<span class="my_business_apply_btn mui-pull-right apply_btn" level=' + data.resp[i].level + ' level_name=' + data.resp[i].level_name + '>申请</span>';
					html += '</a>';
					html += '</li>';
				}
			} else {
				mui.alert(data.msg);
			}
			$('#my_team_mainbox').append(html);

		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
		}
	});

}

//申请时判断等级
mui('body').on('tap', '.apply_btn', function() {
	var level = $(this).attr("level");
	//查询用户信息接口
	mui.ajax(user_url + '/api/userinfo', {
		data: {
			"phone": my_phone,
			"sign": "string",
			"user_id": user_id
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.code == 10000) {
				if(data.resp.is_realname == 0) {
					mui.openWindow({
						url: '../my/name_authentication.html',
						id: 'name_authentication',
						createNew: true
					})
				} else {
					if(data.resp.level > level) {
						mui.alert('您已经是最高等级！')
					}
					if(data.resp.level == level) {
						mui.alert('您已经是' + data.resp.levelString + '，无法申请！')
					}
					if(data.resp.level < level) {
						mui.openWindow({
							url: '../Operator/upgrade_payment.html',
							id: 'upgrade_payment',
							extras: {
								level: level
							},
							createNew: true
						})
					}
				}
			} else {
				mui.alert(data.msg);
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
		}
	});

	//window.top.location.href = "../Operator/upgrade_payment.html";
});

mui.init(); //初始化
mui.ready(function() {
	mui('.mui-scroll-wrapper').scroll({
		bounce: true,
		indicators: true,
		deceleration: mui.os.ios ? 0.003 : 0.0009
	});
	mui('.mui-scroll').on('tap', '.mui-control-item:not(.mui-active)', function() {
		mui('.mui-slider').slider().gotoItem(this.getAttribute('data-index'));
	});
});

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		self = plus.webview.currentWebview();
		levelInfo();
	});
}